package cn.demarcia.coco;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Toast;

import cn.demarcia.coco.ui.activity.BaseActivity;
import cn.demarcia.coco.uihelper.ToastUtils;
import cn.demarcia.coco.uihelper.UIHelper;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;

import static android.view.animation.Animation.AnimationListener;

public class AppStart extends BaseActivity implements AnimationListener {
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = View.inflate(this, R.layout.activity_app_start, null);
        setContentView(view);
        PermissionsDispatcher.checkPermissions(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public void onGetPermissions() {
        startAnim();
        //ApiManager.getInstance().initDeviceId();
    }

    private void startAnim() {
        AlphaAnimation aa = new AlphaAnimation(1.0f, 1.0f);
        aa.setDuration(500);
        view.startAnimation(aa);
        aa.setAnimationListener(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // NOTE: delegate the permission handling to generated method
        PermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @OnShowRationale({Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE})
    void showRationaleForPermission(PermissionRequest request) {
        showRationaleDialog(R.string.permission_rationale, request);
    }

    @OnPermissionDenied({Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE})
    void onPermissionDenied() {
        // NOTE: Deal with a denied permission, e.g. by showing specific UI
        // or disabling certain functionality
        showPermissionsRationaleDialog();
    }

    @OnNeverAskAgain({Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE})
    void onPermissionNeverAskAgain() {
        //showPermissionsRationaleDialog();
        ToastUtils.show(this, "请在应用管理中开启武型吾秀所需的权限", Toast.LENGTH_LONG);
    }

    private void showPermissionsRationaleDialog() {
        showRationaleDialog(R.string.permission_rationale, new PermissionRequest() {
            @Override
            public void proceed() {
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                        Uri.fromParts("package", getPackageName(), null));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }

            @Override
            public void cancel() {
            }
        });
    }

    private void showRationaleDialog(@StringRes int messageResId, final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setPositiveButton(R.string.permission_allow, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton(R.string.permission_deny, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(messageResId)
                .setTitle(R.string.permission_title)
                .show();
    }

    @Override
    public void onAnimationStart(Animation animation) {
        if (!appContext.isNetworkConnected()) {
            ToastUtils.show(this, "网络故障，请检查网络");
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        redirectToHome();
    }


    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    /*
    * 有了引导页之后再用此方法
    * *
        private void redirectToHome() {
            if (appContext.doNotShowNoviceGuideAgain()) {
                UIHelper.redirectToHome(this, appContext);
            } else {
                UIHelper.showNoviceGuide(this);
            }

            finish();
        }*/
    private void redirectToHome() {
        UIHelper.showMainActivity(this);
        finish();
    }
}